<template>
    <div class="wrapper">
        <div class="topWrapper">
            <img class="bg" style="min-height: 150px" src="@/assets/terminalBg.png" alt="" />
            <div class="content">
                <div class="top">
                    <span>{{item.name}}累计收益(元)</span>
                </div>
                <div class="center">
                    <div class="left">
                        ￥
                        <span class="money font-num">{{ profitAll }}</span>
                    </div>
                </div>
                <div class="bottom">
                    <div></div>
                    <div class="btn" @click="selectMon">
                        <span>{{ item.name }}</span>
                        <img src="@/assets/do.png" alt="" />
                    </div>
                </div>
            </div>
        </div>
        <div class="bottomWrapper">
            <div class="content">
                <div class="title">
                    <div class="left">
                        <img src="@/assets/index/icon1.png" alt="" />
                        <span>收益趋势</span>
                    </div>
                    <div class="right"></div>
                </div>
                <div class="echarts">
                    <div id="myChart" :style="{ width: '100%', height: '250px' }"></div>
                </div>
            </div>
        </div>
        <div class="bottomWrapper">
            <div class="content">
                <div class="title">
                    <div class="left">
                        <img src="@/assets/index/icon2.png" alt="" />
                        <span>收益明细</span>
                    </div>
                    <div class="right"></div>
                </div>
                <div class="myTable">
                    <table width="100%">
                        <tr>
                            <th>日期</th>
                            <th>总收益</th>
                           
                            <th>交易</th>
                            <th>返现</th>
                            <th>服务收益</th>
                        </tr>
                        <tr v-for="item in list" :key="item.totalDate">
                            <td>{{ item.totalDate }}</td>
                            <td>{{ item.totalAmount }}</td>
                          
                            <td>{{ item.profitAmount }}</td>
                            <td>{{ item.backAmount }}</td>
                            <td>{{ 0 }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <van-action-sheet @select="onSelect" v-model:show="show" :actions="actions" cancel-text="取消" close-on-click-action />
    </div>
</template>

<script>
import { findProActDayByTime } from '@/api/agent'
import * as echarts from 'echarts'
import { getDay } from '../../utils/utils'
import { findProActMonthByTime, totalStatisticGroupMonth } from '../../api/agent'
export default {
    name: 'monEarnings',
    data() {
        return {
            active: 0,
            profitAll: 0,
            item: { name: '近六个月' },
            show: false,
            list: [],
            mons: [],
            actions: [{ name: '本月' }, { name: '上个月' }, { name: '近六个月' }],
            echartsDay: 7
        }
    },
    mounted() {
        this.onSelect({ name: '近六个月' })
    },
    methods: {
        // 获取卡片数据
        getTotalStatisticGroupMonth() {
            totalStatisticGroupMonth({
                timeEnd: this.mons[0],
                timeStart: this.mons[this.mons.length - 1]
            }).then(res => {
                const { code, message, object } = res
                if (code == 200) {
                    this.profitAll = object.profitTotal
                }
            })
        },
        selectMon(e) {
            this.show = true
        },
        onSelect(item) {
            this.item = item
            //  本月
            if (item.name === '本月') {
                this.mons = []
                var d = new Date()
                var m = d.getMonth() + 1
                m = m < 10 ? '0' + m : m
                this.mons.push(d.getFullYear() + '-' + m)
            }
            //  上月
            if (item.name === '上个月') {
                this.mons = []
                var date = new Date()
                var year = date.getFullYear() //当前年：四位数字
                var month = date.getMonth() //当前月：0-11
                if (month == 0) {
                    //如果是0，则说明是1月份，上一个月就是去年的12月
                    year -= 1
                    month = 12
                }
                month = month < 10 ? '0' + month : month //月份格式化：月份小于10则追加个0

                let lastYearMonth = year + '-' + month
                this.mons = [lastYearMonth]
            }
            // 近六个月
            if (item.name === '近六个月') {
                this.mons = []
                for (var i = 0; i < 6; i++) {
                    var d = new Date()
                    d.setDate(1)
                    d.setMonth(d.getMonth() - i)
                    var m = d.getMonth() + 1
                    m = m < 10 ? '0' + m : m
                    //自定义输出的日期格式
                    this.mons.push(d.getFullYear() + '-' + m)
                }
            }
            this.getFindProActMonthByTime()
            this.getTotalStatisticGroupMonth()
        },
        getFindProActMonthByTime() {
            findProActMonthByTime({
                timeEnd: this.mons[0],
                timeStart: this.mons[this.mons.length - 1]
            }).then(res => {
                let dateList = []
                let valueList = []
                this.list = res.object || []
                res.object.forEach(item => {
                    dateList.push(item.totalDate)
                    valueList.push(item.totalAmount)
                })
                this.showorders(dateList, valueList)
            })
        },
        showorders(dateList, valueList) {
            // 基于准备好的dom，初始化echarts实例
            document.getElementById('myChart').setAttribute('_echarts_instance_', '')

            var chartDom = document.getElementById('myChart')
            if (this.myChart != null && this.myChart != '' && this.myChart != undefined) {
                this.myChart.dispose() //销毁
            }
            this.myChart = echarts.init(chartDom)
            // 指定图表的配置项和数据
            var option
            option = {
                xAxis: {
                    type: 'category',
                    data: dateList
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: valueList,
                        type: 'line',
                        smooth: true,
                        itemStyle: {
                            normal: {
                                color: "#66d6ff", //改变折线点的颜色
                                lineStyle: {
                                    color: "#66d6ff", //改变折线颜色
                                }
                            }
                        }
                    }
                ]
            }
            option && this.myChart.setOption(option)
        }
    }
}
</script>

<style scoped lang="scss">
.wrapper {
    height: 100%;
    display: flex;
    flex-direction: column;
    .topWrapper {
        position: relative;
        font-size: 0;        padding: 10px;
        box-sizing: border-box;

        .bg {
            width: 100%;
        }
        .content {
            position: absolute;
            top: 25px;
            left: 0;
            height: 100%;
            width: 100%;
            padding: 16px 40px;
            box-sizing: border-box;
            color: #ffffff;
            line-height: 35px;
            .top {
                font-size: 16px;
            }
            .center {
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-size: 14px;
                span {
                    font-size: 26px;
                }
            }
            .bottom {
                font-size: 14px;
                display: flex;
                justify-content: space-between;

                .btn {
                    width: 130px;
                    height: 24px;
                    border: 1px solid #ffffff;
                    opacity: 1;
                    color: #ffffff;
                    border-radius: 4px;
                    display: flex;
                    align-items: center;
                    justify-content:center;
                    img {
                        width: 20px;
                    }
                }
            }
        }
    }
    .bottomWrapper {
        flex: 1;
        width: 100%;
        background-color: #ffffff;
        .content {
            border-radius: 10px 10px 0px 0px;
            background-color: #ffffff;
            height: 100%;
            padding: 0 10px;
            .title {
                display: flex;
                align-items: center;
                justify-content: space-between;
                font-size: 0;
                padding: 10px 0;

                border-bottom: 1px solid #e4e4e4;
                .left,
                .right {
                    display: flex;
                    align-items: center;
                }
                .left {
                    img {
                        margin-right: 10px;
                    }
                }
                span {
                    font-size: 14px;
                }
                img {
                    width: 20px;
                }
            }
            .echarts {
                padding-top: 10px;
                .head {
                }
            }
        }
    }
}
</style>
<style lang="scss" scoped>
.van-tab {
    font-size: 12px;
}
table {
    text-align: center;
    margin-top: 10px;
    tr {
        border-bottom: 1px solid rgb(245, 245, 245);
    }
    td,
    th {
        height: 30px;
        width: auto;
    }
}
</style>
